package cn.janescott.inner.space.webapp.controller;

import cn.janescott.inner.space.data.system.dto.UserDTO;
import cn.janescott.inner.space.data.system.facade.SystemFacade;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.util.Map;

/**
 * 测试类
 *
 * @author scott
 * @date 2018/7/30
 */
@Controller
@RequestMapping("/demo")
public class DemoController {

    @Reference(interfaceClass = SystemFacade.class, check = false)
    private SystemFacade systemFacade;

    @RequestMapping("/test")
    public String test() {
        return "Demo";
    }

    @RequestMapping("/test1")
    @ResponseBody
    public UserDTO test1() {
        return systemFacade.findByUsername("scott");
    }

    @RequestMapping("/test2")
    @ResponseBody
    public UserDTO test2(HttpServletRequest request, UserDTO userDTO) throws Exception {

        MultipartHttpServletRequest params = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> files = ((MultipartHttpServletRequest) request).getFileMap();
//        if (null files) {
//
//        }
        MultipartFile file = null;
        BufferedOutputStream stream = null;
//        for (int i = 0; i < files.size(); ++i) {
//            file = files.get(i);
//            if (!file.isEmpty()) {
//                try {
//                    byte[] bytes = file.getBytes();
//                    stream = new BufferedOutputStream(new FileOutputStream(
//                            new File(file.getOriginalFilename())));
//                    stream.write(bytes);
//                    stream.close();
//                } catch (Exception e) {
//                    stream = null;
//                }
//            } else {
//            }
//        }


//        if (null != file && file.getSize() > 0) {
//            InputStream stream = file.getInputStream();
//            File localFile = new File("C:\\Users\\scott\\Desktop\\file\\file1.png");
//            OutputStream out = new FileOutputStream(localFile);
//
//            int index;
//            byte[] bt = new byte[1024];
//            while ((index = stream.read(bt)) != -1) {
//                out.write(bt, 0, index);
//                out.flush();
//            }
//            out.close();
//        }

        return userDTO;
    }

}
